package com.ksc.storekeeper.client.presenter;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.TabData;
import com.gwtplatform.mvp.client.TabDataBasic;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.TabInfo;
import com.gwtplatform.mvp.client.annotations.TitleFunction;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.TabContentProxyPlace;
import com.ksc.storekeeper.client.handlers.PlaceChangeHandler;
import com.ksc.storekeeper.client.mvp.NameTokens;
import com.ksc.storekeeper.client.mvp.StoreKeeperGinInjector;
import com.ksc.storekeeper.client.presenter.StoreAddItemsPresenter.StoreAddItemsProxy;
import com.ksc.storekeeper.client.view.StoreAddItemsView;
import com.ksc.storekeeper.client.view.StoreAddItemsView.StoreAddItemsUiHandler;
import com.ksc.storekeeper.shared.dto.ProductDTO;
import com.ksc.storekeeper.shared.rpc.ProductServiceAsync;

public class StoreAddItemsPresenter extends Presenter<StoreAddItemsView, StoreAddItemsProxy> implements PlaceChangeHandler,StoreAddItemsUiHandler{
	
	@Inject
	PlaceManager placeManager;
	
	@Inject
	ProductServiceAsync productServiceAsync;
	
	@Inject
	public StoreAddItemsPresenter(EventBus eventBus,StoreAddItemsView view,StoreAddItemsProxy proxy){
		super(eventBus,view,proxy);
		getView().setPlaceChangeHandler(this);
		getView().setUiHandlers(this);
	}
	
	@ProxyCodeSplit
	@NameToken(NameTokens.ADD_ITEM)
	public interface StoreAddItemsProxy extends TabContentProxyPlace<StoreAddItemsPresenter>{
		
	}
	
	@TabInfo(container = MyStorePresenter.class)
	static TabData getTabLabel(StoreKeeperGinInjector ginjector) {
		return new TabDataBasic(ginjector.getStoreKeeperConstants().addItems(), 1);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, MyStorePresenter.TYPE_SetTabContent, this);
	}
	
	@Override
	protected void onReveal(){
		super.onReveal();		
	}
	
	@TitleFunction
	public String getTranslatedTitle(StoreKeeperGinInjector gInjector) {
		return gInjector.getStoreKeeperConstants().addItems();
	}

	@Override
	public void changePlace(String token,String name,String value) {
		placeManager.revealPlace(new PlaceRequest(token), false);		
	}
	
	@Override
	public void saveProduct(ProductDTO product){
		productServiceAsync.saveProduct(product, new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				getView().setStatus("Product saved!!");
				getView().reset();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				
				
			}
		});
	}

}
